Base Graphics Assignment - Data Science Course

Submitted 20200703


Dataset Churn


Q1 Create a bar-plot for the ‘PaymentMethod’ column.

a.Assign the color ‘burlywood4’

b.Assign the x-axis label to be “Payment Method”

c.Assign the title to be “Bar-Plot for Payment Method”

plot(churn$PaymentMethod, xlab="Payment Method", main="Bar-Plot for Payment Method", col="burlywood4")


Q2 Create a histogram for the ‘Total Charges’ column.

  1. Assign the color ‘forestgreen’

  2. Assign the x-axis label to be “Total Charges”

  3. Assign the title to be “Histogram for Total Charges”

hist(churn$TotalCharges, xlab="Total Charges", main="Histogram for Total Charges", col="forestgreen")


Q3 Create a density plot for the ‘Total Charges’ column.

  1. Assign the color ‘maroon’

  2. Assign the x-axis label to be “Total Charges”

  3. Assign the title to be “Density plot for Total Charges”

hist(churn$TotalCharges, freq = F, xlab="Total Charges", xlim = c(0,9000), main="Density for Total Charges", col="maroon")

plot(density(churn$TotalCharges), xlab="Total Charges",xlim = c(0,9000),  main="Density for Total Charges", col="maroon",lwd=2)


Q4 Build a histogram for tenure column

  1. Assign the fill color to be ‘mediumspringgreen’

  2. Assign the boundary color to be ‘azure’

  3. Change the number of bins to be 100

hist(churn$tenure, xlab="Tenure",
     main="Histogram for Tenure", col="mediumspringgreen", border="azure", breaks = 100 )


Q5 Build histogram for the ‘MonthlyCharges’ column

  1. Assign ‘PaymentMethod’ to the fill aesthetic
  2. Assign ‘OnlineBackup’ to the fill aesthetic
ggplot(churn, aes(x=MonthlyCharges, fill=PaymentMethod)) +
geom_histogram() +
labs(x =  "Monthly Charges", title ="Monthly charges by Payment Method")

ggplot(churn, aes(x=MonthlyCharges, fill=OnlineBackup)) +
  geom_histogram() +
  labs(x =  "Monthly Charges", title ="Monthly charges by Online Backup")


Q6 Build histogram for the ‘TotalCharges’ column

  1. Assign ‘gender’ to the fill aesthetic
  2. Assign ‘InternetService’ to the fill aesthetic
ggplot(churn, aes(x=TotalCharges, fill=gender)) +
  geom_histogram() +
  labs(x =  "Total Charges", title ="Total charges by Gender")

ggplot(churn, aes(x=TotalCharges, fill=InternetService)) +
  geom_histogram() +
  labs(x =  "Total Charges", title ="Monthly charges by Internet Service")

1. Build a bar-plot for the ‘PhoneService’ column

  1. Assign the fill color to be ‘pink’

  2. Assign the boundary color to be ‘peru’

ggplot(churn,aes(x=PhoneService))+
  geom_bar(fill="pink", color="peru")

  1. Build a bar-plot for the ’InternetService’ column
  1. Assign ‘InternetService’ to the fill aesthetic

  2. Assign ‘Contract’ to the fill aesthetic

  3. Change the position of bars to ‘identity’

ggplot(churn,aes(x=InternetService,fill=churn$Contract))+
  labs(fill="Internet Service")+
  geom_bar(position="identity", alpha=0.4 )

  1. Build a bar-plot for ‘TechSupport’ column
  1. Assign ‘Churn’ to the fill aesthetic
ggplot(churn,aes(x=TechSupport,fill=Churn))+
  labs(fill="Churn")+
  geom_bar()


Q7 Build a scatter-plot between ‘TotalCharges’ & ‘tenure’. Map ‘TotalCharges’ to the y-axis & ‘tenure’ to the ‘x-axis’

  1. Assign it the color ‘wheat3’
  2. Use ‘col’ as an aesthetic and Map ‘PaymentMethod’ to col
  3. Use ‘col’ as an aesthetic and Map ‘gender’ to col
  4. Map ‘Dependents’ to both ‘col’ & ‘shape’ aesthetics
ggplot(churn,aes(y=TotalCharges, x=tenure))+
  labs(x="Tenure", y="Total Charges") +
  geom_point(col="wheat3")

ggplot(churn,aes(y=TotalCharges, x=tenure, col=PaymentMethod))+
  labs(x="Tenure", y="Total Charges", color="Payment Method") +
  geom_point()

ggplot(churn,aes(y=TotalCharges, x=tenure, col=gender))+
  labs(x="Tenure", y="Total Charges", color="Gender") +
  geom_point()

ggplot(churn,aes(y=TotalCharges, x=tenure, col=Dependents, shape=Dependents))+
  labs(x="Tenure", y="Total Charges", color="Dependents") +
  geom_point()


Q8. Build a scatter-plot between ‘tenure’ & ‘MonthlyCharges’. Map ‘tenure’ to the y-axis & ‘MonthlyCharges’ to the ‘x-axis’

  1. Assign it the color ‘yellowgreen’

  2. Use ‘col’ as an aesthetic and Map ‘InternetService’ to col

  3. Use ‘col’ as an aesthetic and Map ‘Contract’

ggplot(churn,aes(y=MonthlyCharges, x=tenure))+
  labs(x="Tenure", y="Monthly Charges") +
  geom_point(col="yellowgreen")

ggplot(churn,aes(y=MonthlyCharges, x=tenure, col=InternetService))+
  labs(x="Tenure", y="Monthly Charges",color="Internet Service") +
  geom_point()

ggplot(churn,aes(y=MonthlyCharges, x=tenure, col=Contract))+
  labs(x="Tenure", y="Monthly Charges",color="Contract") +
  geom_point()

Q8_1. Build a box-plot between ‘tenure’ & ‘Partner’. Map ‘tenure’ to the y-axis & ‘Partner’ to the ‘x-axis’

  1. Assign it a fill color of ‘violet’

  2. Assign it a boundary color of ‘snow3’

ggplot(churn,aes(x=Partner, y=tenure))+
  labs(x="Partner", y="Tenure") +
  geom_boxplot(fill="violet", col="snow3")

Q8_2. Build a box-plot between ‘tenure’ & ‘MultipleLines’. Map ‘tenure’ to the y-axis & ‘MultipleLines’ to the ‘x-axis’

  1. Assign ‘Partner’ to the fill aesthetic

  2. Assign ‘PhoneService’ to the fill aesthetic

ggplot(churn,aes(x=MultipleLines, y=tenure, fill=Partner))+
  labs(x="Multiple Lines", y="Tenure", fill="Partner") +
  geom_boxplot()

ggplot(churn,aes(x=MultipleLines, y=tenure, fill=PhoneService))+
  labs(x="Multiple Lines", y="Tenure", fill="Phone Service") +
  geom_boxplot()

Q8_3. Build a box-plot between ‘tenure’ & ‘Contract’

  1. Assign ‘InternetService’ to the fill aesthetic

  2. Assign ‘PaymentMethod’ to the fill aesthetic

ggplot(churn,aes(x=Contract, y=tenure, fill=InternetService))+
  labs(x="Contract", y="Tenure", fill="Internet Service") +
  geom_boxplot()

ggplot(churn,aes(x=Contract, y=tenure, fill=PaymentMethod))+
  labs(x="Contract", y="Tenure", fill="Payment Method") +
  geom_boxplot()


Q9. Build a box-plot between ‘tenure’ & ‘MultipleLines’. Map ‘tenure’ on the y-axis & ‘MultipleLines’ on the x-axis. Map ‘InternetService’ to the fill aesthetic

ggplot(churn,aes(x=MultipleLines, y=tenure, fill=InternetService))+
  labs(x="Multiple Lines", y="Tenure", fill="Internet Service") +
  geom_boxplot()+
  facet_wrap(~InternetService)


Q10 Build a scatter-plot between ‘TotalCharges’ & ‘tenure’. Map ‘TotalCharges’ on the y-axis & ‘tenure’ on the x-axis. Map ‘Contract’ onto col aesthetic

  1. Facet the plot w.r.t ‘Contract’ column
ggplot(churn,aes(y=TotalCharges, x=tenure, col=Contract))+
  labs(x="Tenure", y="Total Charges", color="Contract") +
  geom_point()+
  facet_wrap(~Contract)


Q11 Build a histogram for the ‘MonthlyCharges’ column. Map ‘PaymentMethod’ onto fill aesthetic.

  1. Facet the plot w.r.t ‘PaymentMethod’ column
ggplot(churn, aes(x=MonthlyCharges, fill=PaymentMethod)) +
  geom_histogram() +
  labs(x =  "Monthly Charges", title ="Monthly charges by Payment Method")+
  facet_wrap(~PaymentMethod)


Q12 Build a bar-plot for the ‘gender’ column. Give it a fill color of ‘blue4’

  1. Give the panel a background color of ‘chartreuse4’
  2. Give the plot a background color of ‘cornsilk4’
ggplot(churn,aes(x=gender))+
  labs(x="Gender")+
  geom_bar(fill="blue")+
  theme(
    panel.background = element_rect(fill="chartreuse4"),
    plot.background = element_rect(fill="cornsilk4")
    )


Q13 Build a scatter-plot between ‘tenure’ & ‘MonthlyCharges’. Map ‘tenure’ on the y-axis & ‘MonthlyCharges’ on the x-axis. Assign a color of ‘yellowgreen’ to the points.

  1. Add a title to the plot ‘Tenure vs Monthly Charges’

  2. Give the panel a background color of ‘coral2’

  3. Give the plot a background color of ‘beige’

  4. Center align the title & make the color of the title to be red

ggplot(churn,aes(x=MonthlyCharges, y=tenure))+
  labs(y="Tenure", x="Monthly Charges", title="Tenure vs Monthly Charges") +
  geom_point(col="yellowgreen")+
  theme(
    panel.background = element_rect(fill="coral2"),
    plot.background = element_rect(fill="beige"),
    plot.title = element_text(hjust = 0.5, colour = "red"),
  )